//
//  AppDelegate+TXIM.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/9/2.
//

import Foundation


extension AppDelegate {
        // 登录IM
    func loginIm() {
        V2TIMManager.sharedInstance()?.login(BHMeUserInfoModel.shared.userId, userSig: BHMeUserInfoModel.shared.userSig, succ: {
            let config = V2TIMAPNSConfig.init()
            config.businessID = Int32.tencentIMBusinessId
            config.token = self.deviceToken
            V2TIMManager.sharedInstance()?.setAPNS(config, succ: {
                print(" ----> 设置APNS 成功")
            }, fail: { (code, errorMsg) in
                print(" ----> 设置APNS 失败")
            })
            
            SVProgressHUD.dismiss()
            // 切换主视图
            DispatchQueue.main.async {
                UIWindow.keyWindow.rootViewController = BHTabbarController.init();
            }
            
        }, fail: { (code, errorMsg) in
            SVProgressHUD.dismiss()
            
            BHToast.toast(text: "登录失败")
            BHMeUserInfoModel.shared.clearUser()
            
            let currentVC = BHBaseViewController.currentViewController
            if currentVC is BHLaunchAdViewController {
                let loginVC = BHLoginViewController.init()
                DispatchQueue.main.async {
                    BHNavigationController.currentNavigationController?.pushViewController(loginVC, animated: true)
                }
            }
        })
        
    }
}



extension AppDelegate {

    func registerIMNotification()  {
        let config = TUIKitConfig.default()
        config!.avatarType = .TAvatarTypeRounded
        TUIKit.sharedInstance()?.setup(withAppId: UInt32(Int32.tencentIMAppId))
        TUIKit.sharedInstance()?.config = config
        
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.badge,.sound]) { (success, error) in
            
        }
        UIApplication.shared.registerForRemoteNotifications()
        
    }
}

// MARK: 处理IM的远程信息
extension AppDelegate {
    func redealIMMessage(dic: [AnyHashable:Any]) {
        let extText = BHJsonTool.hashDecoderString(dic: dic, key: "ext")
        let extParam = extText.transJsonToDic()
        
        let entity = BHJsonTool.hashDecoderDic(dic: extParam, key: "entity")
        if entity.count == 0 {
            return
        }
        // 业务，action : 1 普通文本推送；2 音视频通话推送
        let action = BHJsonTool.decodeIntFromDic(dic: entity, key: "action")
        if action == -1 {
            return
        }
        // 聊天类型，chatType : 1 单聊；2 群聊
        let chatType = BHJsonTool.decodeIntFromDic(dic: entity, key: "chatType")
        if chatType == -1 {
            return
        }
        
        // 目前只有普通通话
        if action == 1 {
            if chatType == 1 {
                self.userId = BHJsonTool.decodeStringFromDic(dic: entity, key: "sender")
                BHRouter.shared.bh_createSingleChat(userId: self.userId, name: nil)
                
                self.userId = ""
            }else if chatType == 2 {
                self.groupId = BHJsonTool.decodeStringFromDic(dic: entity, key: "sender")
                BHRouter.shared.bh_goGroupChat(groupId: self.groupId)
                
                self.groupId = ""

            }
        }
    }
}
